/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 10 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import org.betonthemove.platform.*;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class BetResultView extends Activity
				implements OnClickListener, Runnable {

	public static final String MARKET_DATA_ATTR = "org.betonthemove.MarketData";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.betresultview);
        
        TextView selectionName = (TextView)findViewById(R.id.selectionName);
        TextView oddsDisplay = (TextView)findViewById(R.id.oddsDisplay);
        TextView stakeDisplay = (TextView)findViewById(R.id.stakeDisplay);
        
        m_result = (TextView)findViewById(R.id.result);
        m_closeButton = (Button)findViewById(R.id.closebet);
        
        m_closeButton.setEnabled(false);
        m_closeButton.setOnClickListener(this);
        
        m_marketEntrantData =(MarketData)getIntent().getSerializableExtra(MARKET_DATA_ATTR);
        
        selectionName.setText(m_marketEntrantData.getSelectionName()+
        		" ["+(m_marketEntrantData.isBack()?"Back":"Lay")+"]");
        
        try {
        	m_currency = DisplayHelper.getCurrencySymbol(
        			BettingPlatformFactory.getBettingPlatform().getCurrency());
        } catch(Exception e) {
        }
        
        oddsDisplay.setText("ODDS: "+m_marketEntrantData.getPrice());
        stakeDisplay.setText("STAKE: "+m_currency+
					m_marketEntrantData.getStake());
        
        run();
    }
    
    public void onClick(View button) {
    	if (button == m_closeButton) {
    		finish();
    	}
    }
	
    public void run() {
    	
    	if (m_state == 0) {
    		m_result.setText("Submitting bet....");
    		
    		m_state++;
    		
    		new Thread(this).start();
    	} else if (m_state == 1) {

    		BettingPlatform platform=BettingPlatformFactory.getBettingPlatform();
        
	        try {
		        m_state++;
		        
		        Bet bet=platform.placeBet(m_marketEntrantData.getMarketId(),
		        		m_marketEntrantData.getSelectionId(),
		        		m_marketEntrantData.isBack(),
		        		m_marketEntrantData.getPrice(),
		        		m_marketEntrantData.getStake());
		        
		        m_resultBet = bet;
		        
	        } catch(Exception e) {
	        	m_resultError = e;
	        }
	        
	        runOnUiThread(this);
    	} else if (m_state == 2) {

    		if (m_resultBet != null) {
    			String matched=null;
    			
    			if (m_resultBet.getStake() == m_marketEntrantData.getStake()) {
    				matched = "Fully Matched";
    			} else if (m_resultBet.getStake() > 0) {
    				matched = "Partial Match "+m_currency+
								m_resultBet.getStake();
    			} else {
    				matched = "Unmatched";
    			}
    			
    			String text="ID: "+m_resultBet.getBetId()+
    					"   "+matched;
    			
    			m_result.setText(text);
    		} else if (m_resultError != null) {
    			m_result.setText(m_resultError.getLocalizedMessage());
    		}

    		m_closeButton.setEnabled(true);
    	}
    }
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);		
	}

	private MarketData m_marketEntrantData=null;
    private TextView m_result=null;
    private int m_state=0;
    private Button m_closeButton=null;
    private Bet m_resultBet=null;
    private Exception m_resultError=null;
    private String m_currency="";
}
